import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import urllib
from google.appengine.api import urlfetch

import sys
import httplib
import re
import string

import datastore
from datetime import date

class Standings(db.Model):

 def scrap(self,start,finish):
    def Add(data):
      return ',"' + data + '"'

    # M3.5 listed in order of record
    url = "http://www.ustanorcal.com/standings.asp?a=&l=5882:886&r=L"

    result = urlfetch.fetch(url)
    scraped = result.content

#   Get Teams
    rexp = r"ref=teaminfo\.asp\?id=([\d]*)>([\w\d \/\.\(\\')]*)<"
    r = re.compile(rexp,re.IGNORECASE )
    match = r.findall( scraped )

    teamlist=[]
    nl = "<br>"

    only=0
    for i,team in enumerate(match):
       idx = i
       id = team[0]
       name = team[1]
       g = datastore.Standings( key_name= id )
       g.idx = i
       g.teamid = int(id)
       g.name = name
#      g.level = level
#      g.updated = date.now()   # not needed for

       only = only +1

       if( only < start) : continue
       if( only > finish) : break

       print(g.idx,g.teamid,g.name)
       teamlist.append( g )


#   Put into db with a single call
    db.put( teamlist )
    print( "put " )

 def show(self):
    query="select __key__ from League order by idx"
    keys  = db.GqlQuery( query )
    for k in keys:
     t = db.get(k)
     print t
     print t.name


# ------------------------


class MainPage(webapp.RequestHandler):

  def Write(self,t):
    self.response.out.write(t)

  def get(self):
    start =  self.request.get('start') 
    finish =  self.request.get('finish') 
    show =  self.request.get('show') 

    if(start=="" or finish==""):
      start=0
      finish=5

    print('go')
    cup = Standings()
    if(start!=""):
     cup.scrap(  int(start), int(finish)  )
    elif( show!=""):
     cup.show()

application = webapp.WSGIApplication(
                                     [('/standings', MainPage)],
                                     debug=True)



def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()
